package pl.edu.mimuw.irs.core.model.design;

import java.util.LinkedHashSet;
import java.util.Set;

import pl.edu.mimuw.irs.core.global.IDManager;
import pl.edu.mimuw.irs.core.model.design.Fragmentation.Type;
import pl.edu.mimuw.irs.core.model.petrinet.Net;
import pl.edu.mimuw.irs.core.util.Converter;
import pl.edu.mimuw.irs.core.util.Position;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-07
 */
public class IRSActivity extends IRSTransition {
	
	private static final long serialVersionUID = 6246018324000613033L;

	// *** Attributes
	
	private IRSRequirement requirement;
	private int timeUnits;
	// Node supports
	private final static Set<Fragmentation.Type> supportedFragmentations;
	static {
		supportedFragmentations = new LinkedHashSet<Type>();
		supportedFragmentations.add(Type.SEQUENCE);
		supportedFragmentations.add(Type.ALTERNATIVE);
		supportedFragmentations.add(Type.NONE);
	}
	public IRSRequirement getRequirement() {
		return requirement;
	}
	public void setRequirement(IRSRequirement requirement) {
		this.requirement = requirement;
	}
	@Override
	public Set<Type> getSupportedFragmentations() {
		return supportedFragmentations;
	}
	public int getTimeUnits() {
		return timeUnits;
	}
	
	// *** Constructors

	public IRSActivity(IDManager idManager, String newId, NodeType nodeType, Position position, IRSRequirement requirement) {
		super(idManager, newId, nodeType, position);
		setRequirement(requirement);
		this.timeUnits = 1; // XXX : time units are to be implemented
	}
	
	public IRSActivity(IDManager idManager, String id, Position position, IRSRequirement requirement) {
		super(idManager, id, position);
		setRequirement(requirement);
		this.timeUnits = 1; // XXX : to delete
	}

	public IRSActivity(IDManager idManager, Position position, IRSRequirement requirement) {
		super(idManager, position);
		setRequirement(requirement);
		this.timeUnits = 1; // XXX : to delete
	}
	
	public IRSActivity(IDManager idManager, IRSRequirement requirement) {
		super(idManager);
		setRequirement(requirement);
		this.timeUnits = 1; // XXX : to delete
	}
	
	// *** Methods
	
	@Override
	public void setNet(Net net) {
		super.setNet(net);

		IRSWorkflow workflow = Converter.asIRSWorkflow(getNet());
		workflow.getActivities().add(this);
		workflow.getRequirements().add(requirement);
	}
	
	@Override
	public void destroy() {
		if (getNet() != null) {
			IRSWorkflow workflow = Converter.asIRSWorkflow(getNet());
			workflow.getActivities().remove(this);
			workflow.getRequirements().remove(getRequirement());
		}
		super.destroy();
	}
}
